<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="close">
    <el-form ref="addForm" label-width="120px" :model="addForm" :rules="addRules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model.trim="addForm.name" style="width: 80%;" size="mini" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model.trim="addForm.code" style="width: 80%;" size="mini" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="addForm.managerId" style="width: 80%;" size="mini" placeholder="请选择部门负责人">
          <el-option
            v-for="item in mangerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model.trim="addForm.introduce" style="width: 80%;" size="mini" type="textarea" :rows="4" placeholder="1-100个字符" />
      </el-form-item>
    </el-form>
    <!-- slot="footer" v-slot="{ data }" v-slot:footer #footer === slot='footer' -->
    <!-- slot="footer" v-slot:footer #footer(必须用在template标签上) -->
    <template #footer>
      <el-row style="width: 100%;" type="flex" justify="end">
        <el-button size="mini" type="text" style="color: #727479; margin-right: 20px;" @click="close">取消</el-button>
        <el-button type="primary" size="mini" @click="btnOK">确认</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>
<script>
import { addDeptAPI, getDepartMentAPI, getDepartMentDetailsAPI, getUserListAPI, updateDeparMentAPI } from '@/api'

export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNodeId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      addForm: {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      },
      addRules: {
        code: [
          { required: true, message: '编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '编码为2-10个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              const list = await getDepartMentAPI()
              if (this.addForm.id) {
                list.filter(item => item.id !== this.addForm.id).some(item => item.code === value) ? callback(new Error(`当前部门已有${value}编码`)) : callback()
              } else {
                list.some(item => item.code === value) ? callback(new Error(`当前部门已有${value}编码`)) : callback()
              }
            },
            trigger: 'blur'
          }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍为1-100个字符', trigger: 'blur' }
        ],
        managerId: [
          { required: true, message: '负责人不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称为2-10个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              const list = await getDepartMentAPI()
              if (this.addForm.id) {
                list.filter(item => item.id !== this.addForm.id).some(item => item.name === value) ? callback(new Error(`当前部门已有${value}编码`)) : callback()
              } else {
                list.some(item => item.name === value) ? callback(new Error(`当前部门已有${value}编码`)) : callback()
              }
            },
            trigger: 'blur'
          }
        ]
      },
      mangerList: []
    }
  },
  computed: {
    showTitle() {
      return this.addForm.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    close() {
      this.$emit('update:showDialog', false)
      this.addForm = {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      }
      this.$refs.addForm.resetFields() // 重置表单  resetFields 只能重置在模板中绑定的数据
    },
    async btnOK() {
      await this.$refs.addForm.validate() // 校验通过
      let msg = '新增'
      if (this.addForm.id) {
        await updateDeparMentAPI(this.addForm)
        msg = '编辑'
      } else {
        await addDeptAPI({ ...this.addForm, pid: this.currentNodeId })
      }
      this.$message.success(msg + '部门成功')
      this.close()
      this.$emit('updateDept') // 通知父组件更新
    },
    async getUserList() {
      this.mangerList = await getUserListAPI()
    },
    async getDepartMentDetails(id) {
      this.addForm = await getDepartMentDetailsAPI(id)
    }
  }
}
</script>
